<script setup lang="ts">
import { ref, reactive } from 'vue'

// 拖拽排序相关数据
const items = ref([
  { id: 1, text: '项目 1', color: 'bg-blue-100 border-blue-300' },
  { id: 2, text: '项目 2', color: 'bg-green-100 border-green-300' },
  { id: 3, text: '项目 3', color: 'bg-yellow-100 border-yellow-300' },
  { id: 4, text: '项目 4', color: 'bg-red-100 border-red-300' },
  { id: 5, text: '项目 5', color: 'bg-purple-100 border-purple-300' }
])

// 文件上传相关数据
const fileState = reactive({
  draggedOver: false,
  uploadedFiles: [] as File[],
  isDragging: false
})

// 拖拽排序相关方法
const dragStart = (event: DragEvent, index: number) => {
  if (event.dataTransfer) {
    event.dataTransfer.setData('text/plain', index.toString())
    event.dataTransfer.effectAllowed = 'move'
  }
}

const dragOver = (event: DragEvent) => {
  event.preventDefault()
  if (event.dataTransfer) {
    event.dataTransfer.dropEffect = 'move'
  }
}

const drop = (event: DragEvent, targetIndex: number) => {
  event.preventDefault()
  if (event.dataTransfer) {
    const sourceIndex = parseInt(event.dataTransfer.getData('text/plain'))
    if (sourceIndex !== targetIndex) {
      const [movedItem] = items.value.splice(sourceIndex, 1)
      items.value.splice(targetIndex, 0, movedItem)
    }
  }
}

// 文件上传相关方法
const handleDragOver = (event: DragEvent) => {
  event.preventDefault()
  fileState.draggedOver = true
}

const handleDragLeave = () => {
  fileState.draggedOver = false
}

const handleDrop = (event: DragEvent) => {
  event.preventDefault()
  fileState.draggedOver = false
  
  if (event.dataTransfer && event.dataTransfer.files) {
    const files = Array.from(event.dataTransfer.files)
    fileState.uploadedFiles = [...fileState.uploadedFiles, ...files]
  }
}

const handleFileSelect = (event: Event) => {
  const input = event.target as HTMLInputElement
  if (input.files) {
    const files = Array.from(input.files)
    fileState.uploadedFiles = [...fileState.uploadedFiles, ...files]
  }
}

const removeFile = (index: number) => {
  fileState.uploadedFiles.splice(index, 1)
}

const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return '0 Bytes'
  const k = 1024
  const sizes = ['Bytes', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
</script>

<template>
  <div class="min-h-screen bg-gray-50 py-8 px-4">
    <div class="max-w-4xl mx-auto">
      <!-- 页面标题 -->
      <div class="text-center mb-12">
        <h1 class="text-4xl font-bold text-gray-800 mb-4">拖拽功能演示</h1>
        <p class="text-lg text-gray-600">体验拖拽排序和文件上传功能</p>
      </div>

      <!-- 拖拽排序区域 -->
      <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
        <h2 class="text-2xl font-semibold text-gray-800 mb-6">拖拽排序</h2>
        <p class="text-gray-600 mb-4">拖拽项目进行重新排序</p>
        
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          <div 
            v-for="(item, index) in items" 
            :key="item.id"
            :draggable="true"
            @dragstart="dragStart($event, index)"
            @dragover="dragOver"
            @drop="drop($event, index)"
            class="p-4 rounded-lg border-2 border-dashed cursor-move transition-all duration-300 hover:shadow-md"
            :class="[item.color, fileState.draggedOver ? 'border-blue-400' : '']"
          >
            <div class="flex items-center justify-between">
              <span class="font-medium text-gray-700">{{ item.text }}</span>
              <span class="text-gray-400">⋮⋮</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 文件上传区域 -->
      <div class="bg-white rounded-xl shadow-lg p-6">
        <h2 class="text-2xl font-semibold text-gray-800 mb-6">文件上传</h2>
        
        <!-- 拖拽上传区域 -->
        <div 
          @dragover="handleDragOver"
          @dragleave="handleDragLeave"
          @drop="handleDrop"
          class="border-2 border-dashed rounded-lg p-8 text-center transition-all duration-300 mb-6"
          :class="fileState.draggedOver ? 'border-blue-400 bg-blue-50' : 'border-gray-300 bg-gray-50'"
        >
          <div class="text-4xl mb-4">📁</div>
          <p class="text-lg font-medium text-gray-700 mb-2">拖拽文件到此处上传</p>
          <p class="text-gray-500">或点击下方按钮选择文件</p>
        </div>

        <!-- 文件选择按钮 -->
        <div class="text-center mb-6">
          <label for="file-input" class="inline-flex items-center px-6 py-3 bg-blue-500 text-white rounded-lg cursor-pointer hover:bg-blue-600 transition-colors">
            <span class="mr-2">📎</span>
            选择文件
          </label>
          <input 
            id="file-input"
            type="file" 
            multiple 
            @change="handleFileSelect" 
            class="hidden"
          />
        </div>

        <!-- 已上传文件列表 -->
        <div v-if="fileState.uploadedFiles.length > 0" class="mt-6">
          <h3 class="text-lg font-medium text-gray-700 mb-4">已上传文件</h3>
          <div class="space-y-3">
            <div 
              v-for="(file, index) in fileState.uploadedFiles" 
              :key="index"
              class="flex items-center justify-between p-3 bg-gray-50 rounded-lg"
            >
              <div class="flex items-center">
                <span class="text-xl mr-3">
                  {{ file.type.includes('image') ? '🖼️' : file.type.includes('text') ? '📄' : '📎' }}
                </span>
                <div>
                  <p class="font-medium text-gray-700">{{ file.name }}</p>
                  <p class="text-sm text-gray-500">{{ formatFileSize(file.size) }}</p>
                </div>
              </div>
              <button 
                @click="removeFile(index)"
                class="text-red-500 hover:text-red-700 transition-colors"
              >
                ❌
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- 使用说明 -->
      <div class="mt-8 text-center text-gray-500 text-sm">
        <p>💡 提示：拖拽项目进行排序，拖拽文件到上传区域或点击选择文件按钮</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 自定义拖拽样式 */
[draggable="true"] {
  user-select: none;
}

/* 拖拽时的视觉反馈 */
.drag-over {
  border-color: #3b82f6 !important;
  background-color: #eff6ff !important;
}

/* 平滑过渡效果 */
* {
  transition: all 0.2s ease-in-out;
}
</style>
